package com.ai.zhly.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.common.InitParam;
import com.ai.dmc.index.common.bean.AreaBean;
import com.ai.dmc.index.common.bean.CommonBaseBean;
import com.ai.dmc.index.common.bean.FuncBean;
import com.ai.dmc.index.common.cache.AreaCache;
//import dmc-idx-common.src.main.java.com.ai.dmc.index.common.data.cache.AreaCache.java;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
* @Title: MapDataImpl.java
* @Description: 家装宽带展示层service
* @author xueyf3  
* @date 2016年5月23日 下午4:22:54
* Copyright：Copyright (c) 2016
* Company：北京亚信智慧数据科技有限公司
* @version V1.0
 */
public class CommonServiceImpl extends BaseServiceImpl implements ICommonService{
	// 定义对应Mapper文件的命名空间
    private static final String NAMESPACE = "Common.";
    
    public static Logger log = LoggerFactory.getServiceLog(CommonServiceImpl.class);
    
    /**
	 * 
	 * @Description : 查询所有地域信息，没有数据权限验证，不能直接访问
	 * 
	 * @return Map<String, List<AreaBean>>
	 * 
	 * @author : wucm
	 * @date : 2015年7月27日 上午10:24:52
	 * 
	 */
	public Map<String, List<AreaBean>> _queryArea_() {
		Map<String, List<AreaBean>> map = new HashMap<String, List<AreaBean>>();

		// 地市
		List<AreaBean> cityList = new ArrayList<AreaBean>();
		// 区县
		List<AreaBean> countyList = new ArrayList<AreaBean>();
		// 区域
		List<AreaBean> regionList = new ArrayList<AreaBean>();
		// 区域
		List<AreaBean> subRegionList = new ArrayList<AreaBean>();

		Map<String, String> params = new HashMap<String,String>();
		cityList = this.getBaseDao().queryForList(
                NAMESPACE + "queryAreaCity",params ,AreaBean.class);
		regionList = this.getBaseDao().queryForList(
                NAMESPACE + "queryAreaRegion", params,AreaBean.class);
		// 全部
		List<AreaBean> allList = new ArrayList<AreaBean>();
		allList.addAll(cityList);
		allList.addAll(regionList);

		map.put(AreaBean.TYPE_ALL, allList);
		map.put(AreaBean.TYPE_CITY, cityList);
		map.put(AreaBean.TYPE_COUNTY, countyList);
		map.put(AreaBean.TYPE_REGION, regionList);
		map.put(AreaBean.TYPE_SUB_REGION, subRegionList);

		return map;
	}

	/**
	 * 
	 * @Description : 查询所有地域信息，没有数据权限验证
	 * 
	 * @return Map<String, List<AreaBean>>
	 * 
	 * @author : wucm
	 * @date : 2015年7月27日 上午10:24:52
	 * 
	 */
	public Map<String, List<AreaBean>> queryArea() {
		return _queryArea_();
	}

	/**
	 * 
	 * @Description : 查询所有地域信息，没有数据权限验证
	 * 
	 * @return Map<String, List<AreaBean>>
	 * 
	 * @author : wucm
	 * @date : 2015年7月27日 上午10:24:52
	 * 
	 */
	public Map<String, Map<String, AreaBean>> queryAreaMap() {
		Map<String, Map<String, AreaBean>> r = new HashMap<String, Map<String, AreaBean>>();

		Map<String, List<AreaBean>> am = _queryArea_();

		// 全部
		Map<String, AreaBean> allMap = new HashMap<String, AreaBean>();
		// 地市
		Map<String, AreaBean> cityMap = new HashMap<String, AreaBean>();
		// 区县
		Map<String, AreaBean> countyMap = new HashMap<String, AreaBean>();
		// 区域
		Map<String, AreaBean> regionMap = new HashMap<String, AreaBean>();
		// 子区域
		Map<String, AreaBean> subRegionMap = new HashMap<String, AreaBean>();

		// 地市
		for (AreaBean ab : am.get(AreaBean.TYPE_CITY)) {
			cityMap.put(ab.getAreaId(), ab);
		}
		// 区县
		for (AreaBean ab : am.get(AreaBean.TYPE_COUNTY)) {
			countyMap.put(ab.getAreaId(), ab);
		}
		// 区域
		for (AreaBean ab : am.get(AreaBean.TYPE_REGION)) {
			regionMap.put(ab.getAreaId(), ab);
		}
		// 子区域
		for (AreaBean ab : am.get(AreaBean.TYPE_SUB_REGION)) {
			subRegionMap.put(ab.getAreaId(), ab);
		}

		allMap.putAll(cityMap);
		allMap.putAll(countyMap);
		allMap.putAll(regionMap);
		allMap.putAll(subRegionMap);

		r.put(AreaBean.TYPE_ALL, allMap);
		r.put(AreaBean.TYPE_CITY, cityMap);
		r.put(AreaBean.TYPE_COUNTY, countyMap);
		r.put(AreaBean.TYPE_REGION, regionMap);
		r.put(AreaBean.TYPE_SUB_REGION, subRegionMap);

		return r;
	}

	/**
	 * 
	 * @Description : 查询用户拥有权限的所有地域信息(没有数据权限的被过滤掉)(用途：用于前台展示)
	 * 
	 * @param userId
	 * @return Map<String,List<AreaBean>>
	 * 
	 * @author : wucm
	 * @date : 2015年8月24日 下午2:27:28
	 * 
	 */
	public Map<String, List<AreaBean>> queryAreaForUser(String userId) {
		Map<String, List<AreaBean>> map = new HashMap<String, List<AreaBean>>();
		List<AreaBean> allNew = new ArrayList<AreaBean>();
		List<AreaBean> cityNew = new ArrayList<AreaBean>();
		List<AreaBean> countyNew = new ArrayList<AreaBean>();
		List<AreaBean> regionNew = new ArrayList<AreaBean>();
		List<AreaBean> subRegionNew = new ArrayList<AreaBean>();

		Map<String, List<AreaBean>> m = _queryArea_();
		// 全部
		List<AreaBean> allList = m.get(AreaBean.TYPE_ALL);
		// 地市
		List<AreaBean> cityList = m.get(AreaBean.TYPE_CITY);
		// 区县
		List<AreaBean> countyList = m.get(AreaBean.TYPE_COUNTY);
		// 区域
		List<AreaBean> regionList = m.get(AreaBean.TYPE_REGION);
		// 子区域
		List<AreaBean> subRegionList = m.get(AreaBean.TYPE_SUB_REGION);

		// 数据权限
		Map<String, List<String>> scopeMap = queryUserScope(userId);
		List<String> provs = scopeMap.get(AreaBean.TYPE_PROVINCE);
		List<String> citys = scopeMap.get(AreaBean.TYPE_CITY);
		List<String> countys = scopeMap.get(AreaBean.TYPE_COUNTY);
		List<String> regions = scopeMap.get(AreaBean.TYPE_REGION);
		List<String> subRegions = scopeMap.get(AreaBean.TYPE_SUB_REGION);

		// 省级数据权限--默认返回所有
		if (provs.size() > 0) {
			for (AreaBean b : allList) {
				b.setHasRight(true);
			}
			for (AreaBean b : cityList) {
				b.setHasRight(true);
			}
			for (AreaBean b : countyList) {
				b.setHasRight(true);
			}
			for (AreaBean b : regionList) {
				b.setHasRight(true);
			}
			for (AreaBean b : subRegionList) {
				b.setHasRight(true);
			}

			map.put(AreaBean.TYPE_ALL, allList);
			map.put(AreaBean.TYPE_CITY, cityList);
			map.put(AreaBean.TYPE_COUNTY, countyList);
			map.put(AreaBean.TYPE_REGION, regionList);
			map.put(AreaBean.TYPE_SUB_REGION, subRegionList);

			return map;
		}

		// 市级数据权限
		if (citys.size() > 0) {
			for (String areaId : citys) {
				for (AreaBean b : cityList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						cityNew.add(b);
					}
				}
				for (AreaBean b : countyList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						countyNew.add(b);
					}
				}
				for (AreaBean b : regionList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						regionNew.add(b);
					}
				}
				for (AreaBean b : subRegionList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						subRegionNew.add(b);
					}
				}
			}
		}

		// 区县级数据权限
		if (countys.size() > 0) {
			for (String areaId : countys) {
				for (AreaBean b : countyList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						countyNew.add(b);
					}
				}
				for (AreaBean b : regionList) {
					String aId = b.getAreaId();
					String upId = b.getUpperId();
					if (aId.indexOf(areaId) != -1 || upId.equals(areaId)) { // 有些region_id编码的前4位不是county_id编码
						regionNew.add(b);
					}
				}
				for (AreaBean b : subRegionList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						subRegionNew.add(b);
					}
				}
			}
		}

		// 区域级数据权限
		if (regions.size() > 0) {
			for (String areaId : regions) {
				for (AreaBean b : regionList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						regionNew.add(b);
					}
				}
				for (AreaBean b : subRegionList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						subRegionNew.add(b);
					}
				}
			}
		}

		// 子区域级数据权限
		if (subRegions.size() > 0) {
			for (String areaId : subRegions) {
				for (AreaBean b : subRegionList) {
					String aId = b.getAreaId();
					if (aId.indexOf(areaId) != -1) {
						subRegionNew.add(b);
					}
				}
			}
		}

		// 数据排重
		Set<AreaBean> citySet = new LinkedHashSet<AreaBean>(cityNew);
		Set<AreaBean> countySet = new LinkedHashSet<AreaBean>(countyNew);
		Set<AreaBean> regionSet = new LinkedHashSet<AreaBean>(regionNew);
		Set<AreaBean> subRegionSet = new LinkedHashSet<AreaBean>(subRegionNew);

		cityNew = new ArrayList<AreaBean>(citySet);
		countyNew = new ArrayList<AreaBean>(countySet);
		regionNew = new ArrayList<AreaBean>(regionSet);
		subRegionNew = new ArrayList<AreaBean>(subRegionSet);

		allNew.addAll(cityNew);
		allNew.addAll(countyNew);
		allNew.addAll(regionNew);
		allNew.addAll(subRegionNew);

		map.put(AreaBean.TYPE_ALL, allNew);
		map.put(AreaBean.TYPE_CITY, cityNew);
		map.put(AreaBean.TYPE_COUNTY, countyNew);
		map.put(AreaBean.TYPE_REGION, regionNew);
		map.put(AreaBean.TYPE_SUB_REGION, subRegionNew);

		return map;
	}

	/**
	 * 
	 * @Description : 查询用户拥有权限的所有地域信息(没有数据权限不被过滤掉，只是将hasRight置为false)
	 * 
	 *              用途：用于前台展示
	 * 
	 * @param userId
	 * @return Map<String,List<AreaBean>>
	 * 
	 * @author : wucm
	 * @date : 2015年8月24日 下午2:27:28
	 * 
	 */
	public Map<String, List<AreaBean>> queryAreaAllForUser(String userId) {
		Map<String, List<AreaBean>> map = new HashMap<String, List<AreaBean>>();
		List<AreaBean> allNew = new ArrayList<AreaBean>();
		List<AreaBean> cityNew = new ArrayList<AreaBean>();
		List<AreaBean> countyNew = new ArrayList<AreaBean>();
		List<AreaBean> regionNew = new ArrayList<AreaBean>();
		List<AreaBean> subRegionNew = new ArrayList<AreaBean>();

		Map<String, List<AreaBean>> m = _queryArea_();
		// 全部
		List<AreaBean> allList = m.get(AreaBean.TYPE_ALL);
		// 地市
		List<AreaBean> cityList = m.get(AreaBean.TYPE_CITY);
		// 区县
		List<AreaBean> countyList = m.get(AreaBean.TYPE_COUNTY);
		// 区域
		List<AreaBean> regionList = m.get(AreaBean.TYPE_REGION);
		// 子区域
		List<AreaBean> subRegionList = m.get(AreaBean.TYPE_SUB_REGION);

		// 数据权限
		Map<String, List<String>> scopeMap = queryUserScope(userId);
		List<String> provs = scopeMap.get(AreaBean.TYPE_PROVINCE);
		List<String> citys = scopeMap.get(AreaBean.TYPE_CITY);
		List<String> countys = scopeMap.get(AreaBean.TYPE_COUNTY);
		List<String> regions = scopeMap.get(AreaBean.TYPE_REGION);
		List<String> subRegions = scopeMap.get(AreaBean.TYPE_SUB_REGION);

		// 省级数据权限--默认返回所有
		if (provs.size() > 0) {
			for (AreaBean b : allList) {
				b.setHasRight(true);
			}
			for (AreaBean b : cityList) {
				b.setHasRight(true);
			}
			for (AreaBean b : countyList) {
				b.setHasRight(true);
			}
			for (AreaBean b : regionList) {
				b.setHasRight(true);
			}
			for (AreaBean b : subRegionList) {
				b.setHasRight(true);
			}

			map.put(AreaBean.TYPE_ALL, allList);
			map.put(AreaBean.TYPE_CITY, cityList);
			map.put(AreaBean.TYPE_COUNTY, countyList);
			map.put(AreaBean.TYPE_REGION, regionList);
			map.put(AreaBean.TYPE_SUB_REGION, subRegionList);

			return map;
		}

		// 市级数据权限
		for (AreaBean b : cityList) {
			Boolean hasRight = false;
			for (String areaId : citys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			b.setHasRight(hasRight);
			cityNew.add(b);
		}

		// 区县级数据权限
		for (AreaBean b : countyList) {
			Boolean hasRight = false;
			for (String areaId : countys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			for (String areaId : citys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			b.setHasRight(hasRight);
			countyNew.add(b);
		}

		// 区域级数据权限
		for (AreaBean b : regionList) {
			Boolean hasRight = false;
			for (String areaId : regions) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			for (String areaId : countys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			for (String areaId : citys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			b.setHasRight(hasRight);
			regionNew.add(b);
		}

		// 子区域级数据权限
		for (AreaBean b : subRegionList) {
			Boolean hasRight = false;
			for (String areaId : subRegions) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			for (String areaId : regions) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			for (String areaId : countys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			for (String areaId : citys) {
				String aId = b.getAreaId();
				if (aId.indexOf(areaId) != -1) {
					hasRight = true;
					break;
				}
			}
			b.setHasRight(hasRight);
			subRegionNew.add(b);
		}

		// 数据排重
		Set<AreaBean> citySet = new LinkedHashSet<AreaBean>(cityNew);
		Set<AreaBean> countySet = new LinkedHashSet<AreaBean>(countyNew);
		Set<AreaBean> regionSet = new LinkedHashSet<AreaBean>(regionNew);
		Set<AreaBean> subRegionSet = new LinkedHashSet<AreaBean>(subRegionNew);

		cityNew = new ArrayList<AreaBean>(citySet);
		countyNew = new ArrayList<AreaBean>(countySet);
		regionNew = new ArrayList<AreaBean>(regionSet);
		subRegionNew = new ArrayList<AreaBean>(subRegionSet);

		allNew.addAll(cityNew);
		allNew.addAll(countyNew);
		allNew.addAll(regionNew);
		allNew.addAll(subRegionNew);

		map.put(AreaBean.TYPE_ALL, allNew);
		map.put(AreaBean.TYPE_CITY, cityNew);
		map.put(AreaBean.TYPE_COUNTY, countyNew);
		map.put(AreaBean.TYPE_REGION, regionNew);
		map.put(AreaBean.TYPE_SUB_REGION, subRegionNew);

		return map;
	}

	/**
	 * 
	 * @Description : 查询用户拥有权限的所有地域信息(没有数据权限的被过滤掉)(用途：用于权限验证)
	 * 
	 * @param userId
	 * @return Map<String,List<AreaBean>>
	 * 
	 * @author : wucm
	 * @date : 2015年8月24日 下午2:27:28
	 * 
	 */
	public Map<String, Map<String, String>> queryAreaIdForUser(String userId) {
		Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
		Map<String, String> allNew = new HashMap<String, String>();
		Map<String, String> cityNew = new HashMap<String, String>();
		Map<String, String> countyNew = new HashMap<String, String>();
		Map<String, String> regionNew = new HashMap<String, String>();
		Map<String, String> subRegionNew = new HashMap<String, String>();

		Map<String, List<AreaBean>> m = queryAreaForUser(userId);

		// 全部
		List<AreaBean> AllList = m.get(AreaBean.TYPE_ALL);
		// 地市
		List<AreaBean> cityList = m.get(AreaBean.TYPE_CITY);
		// 区县
		List<AreaBean> countyList = m.get(AreaBean.TYPE_COUNTY);
		// 区域
		List<AreaBean> regionList = m.get(AreaBean.TYPE_REGION);
		// 子区域
		List<AreaBean> subRegionList = m.get(AreaBean.TYPE_SUB_REGION);

		for (AreaBean b : AllList) {
			String areaId = b.getAreaId();
			allNew.put(areaId, areaId);
		}
		for (AreaBean b : cityList) {
			String areaId = b.getAreaId();
			cityNew.put(areaId, areaId);
		}
		for (AreaBean b : countyList) {
			String areaId = b.getAreaId();
			countyNew.put(areaId, areaId);
		}
		for (AreaBean b : regionList) {
			String areaId = b.getAreaId();
			regionNew.put(areaId, areaId);
		}
		for (AreaBean b : subRegionList) {
			String areaId = b.getAreaId();
			subRegionNew.put(areaId, areaId);
		}

		map.put(AreaBean.TYPE_ALL, allNew);
		map.put(AreaBean.TYPE_CITY, cityNew);
		map.put(AreaBean.TYPE_COUNTY, countyNew);
		map.put(AreaBean.TYPE_REGION, regionNew);
		map.put(AreaBean.TYPE_SUB_REGION, subRegionNew);

		return map;
	}

	/**
	 * 
	 * @Description : 查询用户的数据权限
	 * @param userId
	 * @return
	 * @return Map<String,List<String>>
	 * 
	 * @author : wucm
	 * @date : 2015年8月24日 下午5:10:47
	 * 
	 */
	public Map<String, List<String>> queryUserScope(String userId) {
		Map<String, List<String>> m = new HashMap<String, List<String>>();

		// 用数据权限
		List<AreaBean> scopeList = null;

		Map<String, String> params = new HashMap<String,String>();
		params.put("userId", userId);
		scopeList = this.getBaseDao().queryForList(
                NAMESPACE + "queryUserScopeList", params,AreaBean.class);

		Map<String, List<AreaBean>> allArea = this._queryArea_();

		List<String> alls = new ArrayList<String>();// 全部
		List<String> provs = new ArrayList<String>();// 省级
		List<String> citys = new ArrayList<String>();// 市级
		List<String> countys = new ArrayList<String>();// 区县级
		List<String> regions = new ArrayList<String>();// 区域级
		List<String> subRegions = new ArrayList<String>();// 子区域级
		StringBuffer sb = new StringBuffer();
		for (AreaBean b : scopeList) {
			String areaId = b.getAreaId();
			sb.append(areaId + ",");
		}
		String prov_id = InitParam.DEPLOY_PROVINCE_ID + ",";
		if (sb.toString().indexOf(prov_id) >= 0) {

			provs.add(InitParam.DEPLOY_PROVINCE_ID);

			List<AreaBean> allList = allArea.get(AreaBean.TYPE_ALL);
			for (AreaBean bean : allList) {
				String id = bean.getAreaId();
				alls.add(id);
			}
			List<AreaBean> cityList = allArea.get(AreaBean.TYPE_CITY);
			for (AreaBean bean : cityList) {
				String id = bean.getAreaId();
				citys.add(id);
			}
			List<AreaBean> countysList = allArea.get(AreaBean.TYPE_COUNTY);
			for (AreaBean bean : countysList) {
				String id = bean.getAreaId();
				countys.add(id);
			}
			List<AreaBean> regionsList = allArea.get(AreaBean.TYPE_REGION);
			for (AreaBean bean : regionsList) {
				String id = bean.getAreaId();
				regions.add(id);
			}
			List<AreaBean> subRegionsList = allArea
					.get(AreaBean.TYPE_SUB_REGION);
			for (AreaBean bean : subRegionsList) {
				String id = bean.getAreaId();
				subRegions.add(id);
			}

		} else {
			for (AreaBean b : scopeList) {
				String areaId = b.getAreaId();
				if (null == areaId || areaId.length() == 0) {
					continue;
				}

				// 全部
				alls.add(areaId);

				// 省级
				if (InitParam.DEPLOY_PROVINCE_ID.equals(areaId)) {
					provs.add(areaId);
				}
				// 市级
				if (areaId.length() == 3) {
					citys.add(areaId);
				}
				// 区县级
				if (areaId.length() == 4) {
					countys.add(areaId);
				}
				// 区域级
				if (areaId.length() == 6) {
					regions.add(areaId);
				}
				// 子区域级
				if (areaId.length() == 8) {
					subRegions.add(areaId);
				}
			}

		}

		m.put("all", alls);
		m.put(AreaBean.TYPE_PROVINCE, provs);
		m.put(AreaBean.TYPE_CITY, citys);
		m.put(AreaBean.TYPE_COUNTY, countys);
		m.put(AreaBean.TYPE_REGION, regions);
		m.put(AreaBean.TYPE_SUB_REGION, subRegions);

		return m;
	}

	/**
	 * 
	 * @Description : 查询地市树
	 * @return
	 * @return JSONArray
	 * 
	 * @author : wucm
	 * @param areaId
	 * @param isAdmin
	 * @param userId
	 * @date : 2015年8月11日 下午5:09:57
	 * 
	 */
	public JSONArray queryAreaTree(String userId, Boolean isAdmin,
			String areaId) {
		JSONArray jArr = new JSONArray();
		// 查询数据
		// 地市
		List<AreaBean> cityList = null;
		
		cityList = this.getBaseDao().queryForList(
                NAMESPACE + "queryAreaCity", null,AreaBean.class);

		if (!isAdmin && !InitParam.DEPLOY_PROVINCE_ID.equals(areaId)) {
			List<AreaBean> rList = new ArrayList<AreaBean>();
			for (AreaBean bean : cityList) {
				String area_id = bean.getAreaId();
				if (area_id.equals(areaId) || areaId.indexOf(area_id) >= 0) {
					// areaId 可能是市级以下的编码，可以包含area_id
					rList.add(bean);
				}
			}
			cityList = rList;
		}
		// 省
		JSONObject j = new JSONObject();
		j.put("id", InitParam.DEPLOY_PROVINCE_ID);
		j.put("name", InitParam.DEPLOY_PROVINCE_NAME);
		j.put("pId", "-1");
		jArr.add(j);

		// 地市
		for (AreaBean ab : cityList) {
			j = new JSONObject();
			String id = ab.getAreaId();
			String name = ab.getAreaName();
			String upper_id = InitParam.DEPLOY_PROVINCE_ID;
			j.put("id", id);
			j.put("name", name);
			j.put("pId", upper_id);
			jArr.add(j);
		}

		return jArr;
	}

	/**
	 * 
	 * @Description : 查询数据刷新时间
	 * @param regionId
	 * @return
	 * @return JSONArray
	 * 
	 * @author : wucm
	 * @param warnDB
	 * @date : 2015年7月7日 下午4:17:41
	 * 
	 */
	public Map<String, String> queryDataTime() {
		Map<String, String> m = new HashMap<String, String>();
		List<CommonBaseBean> rList = null;
		rList = this.getBaseDao().queryForList(
                NAMESPACE + "queryDataTime", null,CommonBaseBean.class);
		if (rList != null && rList.size() > 0) {
			for (CommonBaseBean bean : rList) {
				String table_name = bean.getQuery_value1();
				String datetime = bean.getQuery_value2();
				m.put(table_name, datetime);
			}
		}
		return m;
	}

	/**
	 * 
	 * @Description : 查询全量地市，但是判断用户是否具有数据权限
	 * @param userId
	 * @return
	 * @return List<AreaBean>
	 * 
	 * @author : wucm
	 * @date : 2015年7月27日 上午11:28:14
	 * 
	 */
	public void queryAreaCityForUser(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		List<AreaBean> cList = new ArrayList<AreaBean>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<JSONObject> rl = new ArrayList<JSONObject>(); 

		String userId = inputObject.getParams().get("userId");
		// 所有地市
		Map<String, List<AreaBean>> m = _queryArea_();
		List<AreaBean> citys = m.get(AreaBean.TYPE_CITY);

		// 数据权限
		Map<String, List<String>> scopeMap = this.queryUserScope(userId);
		List<String> allList = scopeMap.get(AreaBean.TYPE_ALL);

		// 权限验证
		for (AreaBean ab : citys) {
			String areaId = ab.getAreaId();
			for (String aId : allList) {
				aId = aId.substring(0, 3);
				if (aId.equals(areaId)) {
					ab.setHasRight(true);
					break;
				}
			}
			cList.add(ab);
		}
        if (cList != null && cList.size() > 0) {
        	AreaBean bean = null;
			for (int i = 0; i < cList.size(); i++) {
				bean = cList.get(i);
				String areaId = bean.getAreaId();
				String areaName = bean.getAreaName();
				String coreLongitude = bean.getCoreLongitude();
				String coreLatitude = bean.getCoreLatitude();
				String enableFlag = bean.getEnableFlag();
				String chartFlag = bean.getChartFlag();
				JSONObject tmpObj = new JSONObject();
				tmpObj.put("areaId", areaId);
				tmpObj.put("areaName", areaName);
				tmpObj.put("coreLongitude", coreLongitude);
				tmpObj.put("coreLatitude", coreLatitude);
				tmpObj.put("enableFlag", enableFlag);
				tmpObj.put("chartFlag", chartFlag);
				tmpObj.put("hasRight", true);
				rl.add(tmpObj);
			}
        }

		resultMap.put("regions", null == rl ? "" : JsonUtil.Object2Json(rl));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}

	/**
	 * 
	 * @Description : 查询全量地市，不做数据权限校验
	 * @return
	 * @return List<AreaBean>
	 * 
	 * @author : wucm
	 * @date : 2015年7月27日 上午11:28:14
	 * 
	 */
	public List<AreaBean> queryAreaCity() {
		// 所有地市
		Map<String, List<AreaBean>> m = _queryArea_();
		List<AreaBean> citys = m.get(AreaBean.TYPE_CITY);

		return citys;
	}

	/**
	 * 
	 * @Description : 数据格式转换：地市信息转换
	 * 
	 * @return JSONArray
	 * 
	 * @author : wucm
	 * @date : 2015年11月11日 下午4:49:38
	 * 
	 */
	public JSONArray changeAreaMapToJson(List<AreaBean> areaList) {
		JSONArray jArr = new JSONArray();

		if (null == areaList) {
			return jArr;
		}

		for (AreaBean a : areaList) {
			if (null == a) {
				continue;
			}

			JSONObject j = a.toJSON();
			jArr.add(j);
		}

		return jArr;
	}

	/**
	 * 
	 * @Description : 查询地市树
	 * 
	 * @return JSONArray
	 * 
	 * @author : wucm
	 * @date : 2015年11月13日 下午2:38:00
	 * 
	 */
	public JSONArray queryAreaTree(String userAreaId, Boolean isAdmin) {
		JSONArray jArr = new JSONArray();

		if (null == userAreaId) {
			return jArr;
		}

		// 地市信息
		Map<String, AreaBean> cityMap = AreaCache.areaCache
				.get(AreaBean.TYPE_CITY);
		Map<String, AreaBean> countyMap = AreaCache.areaCache
				.get(AreaBean.TYPE_COUNTY);
		Map<String, AreaBean> regionMap = AreaCache.areaCache
				.get(AreaBean.TYPE_REGION);

		// 省
		JSONObject j = new JSONObject();
		j.put("id", InitParam.DEPLOY_PROVINCE_ID);
		j.put("name", InitParam.DEPLOY_PROVINCE_NAME);
		j.put("pId", "-1");
		if (!isAdmin && !userAreaId.equals(InitParam.DEPLOY_PROVINCE_ID)) {
			j.put("nocheck", true);
		}
		jArr.add(j);

		// 地市
		for (AreaBean ab : cityMap.values()) {
			j = new JSONObject();
			String id = ab.getAreaId();
			String name = ab.getAreaName();
			String upper_id = InitParam.DEPLOY_PROVINCE_ID;
			j.put("id", id);
			j.put("name", name);
			j.put("pId", upper_id);

			if (isAdmin || userAreaId.equals(InitParam.DEPLOY_PROVINCE_ID)
					|| id.contains(userAreaId)) {
				jArr.add(j);
			}

		}

		// 区县
		for (AreaBean ab : countyMap.values()) {
			j = new JSONObject();
			String id = ab.getAreaId();
			String name = ab.getAreaName();
			String upper_id = ab.getUpperId();
			j.put("id", id);
			j.put("name", name);
			j.put("pId", upper_id);

			if (isAdmin || userAreaId.equals(InitParam.DEPLOY_PROVINCE_ID)
					|| id.contains(userAreaId)) {
				jArr.add(j);
			}
		}

		// 景区
		for (AreaBean ab : regionMap.values()) {
			j = new JSONObject();
			String id = ab.getAreaId();
			String name = ab.getAreaName();
			String upper_id = ab.getUpperId();
			j.put("id", id);
			j.put("name", name);
			j.put("pId", upper_id);

			if (isAdmin || userAreaId.equals(InitParam.DEPLOY_PROVINCE_ID)
					|| id.contains(userAreaId)) {
				jArr.add(j);
			}
		}
		return jArr;
	}

	/**
	 * 
	 * @Description : 查询用户的所有功能编码
	 * @return
	 * @return JSONArray
	 * 
	 * @author : wucm
	 * @date : 2015年8月11日 下午5:09:57
	 * 
	 */
	public Set<String> queryFuncIdAllByUser(String userId) {
		Set<String> rset = new HashSet<String>();
		// 查询数据
		List<String> list = null;
		
		list = this.getBaseDao().queryForList(
                NAMESPACE + "queryFuncIdAllByUser", null,String.class);

		for (String funcId : list) {
			rset.add(funcId);
		}

		return rset;
	}
	
	/**
	 * 
	 * @Description : 查询所有地市区域
	 * @return void
	 * 
	 * @author : wucm
	 * @date : 2015年6月30日 下午4:00:34
	 * 
	 */
	public void queryCityRegionAll(InputObject inputObject, OutputObject outputObject) throws BusinessException {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		String userId = inputObject.getParams().get("userId");
		Map<String, List<AreaBean>> aMap = this.queryAreaForUser(userId);
		JSONObject j = new JSONObject();
		j.put(AreaBean.TYPE_CITY,
				this.changeAreaMapToJson((aMap.get(AreaBean.TYPE_CITY))));
		j.put(AreaBean.TYPE_REGION,
				this.changeAreaMapToJson(aMap.get(AreaBean.TYPE_REGION)));
		j.put(AreaBean.TYPE_COUNTY, new JSONArray());

		resultMap.put("result", null == j ? "" : JsonUtil.Object2Json(j));
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}

	/**
	 * 
	 * @Description : 查询首页导航
	 * @param userId
	 * @param isAdmin
	 * @return
	 * @return JSONObject
	 * 
	 * @author : wucm
	 * @date : 2015年10月13日 下午5:59:37
	 * 
	 */
	@Override
	public void queryIndexNav(InputObject inputObject, OutputObject outputObject) throws BusinessException {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		JSONArray jr = new JSONArray();
		String userId = inputObject.getParams().get("userId");
		boolean isAdmin = inputObject.getParams().get("isAdmin")==null?false:Boolean.valueOf(inputObject.getParams().get("isAdmin"));
		// 查询数据
		List<FuncBean> firstList = null;
		List<FuncBean> secondList = null;
		List<String> linkList = null;
		try {
			firstList = this.getBaseDao().queryForList(
	                NAMESPACE + "queryIndexNavFirst", inputObject.getParams(),FuncBean.class);
			secondList = this.getBaseDao().queryForList(
	                NAMESPACE + "queryIndexNavSecond", inputObject.getParams(),FuncBean.class);
			linkList = this.getBaseDao().queryForList(
	                NAMESPACE + "queryFuncIdAllByUser", inputObject.getParams(),String.class);
					
		} catch (Exception e) {
			e.printStackTrace();
		} 

		// 权限过滤
		if (!isAdmin) {
			// 功能过滤
			Iterator<FuncBean> it = secondList.iterator();
			while (it.hasNext()) {
				String funcId = it.next().getFuncId();
				if (!linkList.contains(funcId)) {
					it.remove();
				}
			}

			// 文件夹过滤
			it = firstList.iterator();
			while (it.hasNext()) {
				FuncBean fBean =  it.next();
				String funcId =fBean.getFuncId();
				String funcUrl = fBean.getFuncUrl();
				if (funcUrl!=null&&!funcUrl.trim().equals("")) {
					if (!linkList.contains(funcId)) {
						it.remove();
					}
					continue;
				}
//				if (!linkList.contains(funcId)) {
//					it.remove();
//				}
				
				Boolean flag = false;
				for (FuncBean f : secondList) {
					if (funcId.equals(f.getParentFuncId())) {
						flag = true;
						break;
					}
				}
				if (!flag) {
					it.remove();
				}
			}
		}

		// 数据处理
		for (FuncBean f : firstList) {
			// 一级导航
			JSONObject j = JSONObject.fromObject(f);

			String funcId = f.getFuncId();

			List<FuncBean> subs = new ArrayList<FuncBean>();
			for (FuncBean fb : secondList) {
				// 二级导航
				String parentFuncId = fb.getParentFuncId();
				if (funcId.equals(parentFuncId)) {
					subs.add(fb);
				}
			}
			j.put("sub", JSONArray.fromObject(subs));

			jr.add(j);
		}
		
		resultMap.put("result", null == jr ? "" : jr);
        outputObject.setObject(JsonUtil.Object2Json(resultMap));	
	}

}
